// pages/user/user.js
const util = require("../../utils/util.js")

Page({
  data: {
    menuLists: [{
        index: 0,
        leftIcon: 'order',
        leftLabel: '我的订单',
        rightLabel: '',
        rightIcon: 'arrow-right'
      },
      {
        index: 1,
        leftIcon: 'phone',
        leftLabel: '客服热线',
        rightLabel: '',
        rightIcon: 'arrow-right'
      },
      {
        index: 2,
        leftIcon: 'address',
        leftLabel: '设置默认收货地址',
        rightLabel: '',
        rightIcon: 'arrow-right'
      }
    ],
    // 默认关闭授权弹框
    authDialogClosed: true,
  },

  /**
   * 加载数据
   */
  loadData: function() {
    const this_ = this
    util.requestByPost("/gdd/xcx/selectUser", {}, {
        isLogin: false,
        isPrePage: true
      })
      .then(res => {
        this_.setData({
          phone: res[0].phone
        });
      })
  },

  /**
   * 页面加载时触发。一个页面只会调用一次，可以在 onLoad 的参数中获取打开当前页面路径中的参数
   */
  onLoad: function(options) {
    const this_ = this
    util.getUserInfo().then(res => {
      getApp().globalData.userInfo = res
      this_.setData({
        userInfo: res
      })
    })
    this.loadData() // 加载数据
  },

  /**
   * 用户授权登陆
   */
  onGetUserinfo: function(event) {
    if (event.detail.userInfo) {
      getApp().globalData.userInfo = event.detail.userInfo
      this.setData({
        userInfo: event.detail.userInfo
      })
      let wxUser = this.data.userInfo
      wxUser.encryptedData = event.detail.encryptedData
      wxUser.iv = event.detail.iv
      wxUser.signature = event.detail.signature
      util.requestByPost('/gdd/xcx/modifyWxUser', wxUser, {
        isLogin: true,
        isPrePage: false
      })
    }
  },

  /**
   * 个人中心list列表点击事件
   */
  onClickList: function(event) {
    var this_ = this
    var index = event.currentTarget.dataset.index
    if (index == "0") {
      // 我的订单
      if (getApp().globalData.userInfo) {
        wx.navigateTo({
          url: '/pages/user/order/order?index=0',
        })
      } else {
        wx.showToast({
          title: "您还没有登陆",
          icon: "none",
          mask: true
        })
      }
    } else if (index == "1") {
      // 客服热线
      wx.makePhoneCall({
        phoneNumber: this_.data.phone
      })
    } else if (index == "2") {
      // 设置默认收货地址
      let address = null
      util.chooseAddress(this).then(res => {
        if (typeof res === 'object') {
          address = res.provinceName + res.cityName + res.countyName + "," + res.detailInfo + "," + res.userName + "," + res.telNumber + "," + res.postalCode
          util.requestByPost('/gdd/xcx/modifyWxUser', {
            address: address
          }, {
            isLogin: true,
            isPrePage: false
          }).then(res => {
            wx.setStorageSync("address", address)
            wx.showToast({
              title: "设置成功",
              icon: "none",
              duration: 1500,
              mask: true
            })
          })
        }
      })
    }
  },
  /**
   * 拒绝授权
   */
  onRefuseAuth: function() {
    this.setData({
      authDialogClosed: true
    });
  },
  /**
   * 同意授权
   */
  onAcceptAuth: function() {
    this.setData({
      authDialogClosed: true
    });
  }


})